package com.englisheveryday.activity;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.englisheveryday.R;
import com.englisheveryday.utils.L;
import com.englisheveryday.vo.CategoryVO;

/**
 * category 选择
 * @author cheney
 *
 */
public class CategoryDialog extends AlertDialog {
	
	private ListView listView;
	private OnDialogItemClickListener onDialogItemClickListener;
	private List<CategoryVO> categoryList;
	private ListAdapter adapter;
	
	public CategoryDialog(Context context,int theme,List<CategoryVO> categoryList){
		super(context,theme);
		this.categoryList = categoryList;
	}
	
	public CategoryDialog(Context context,List<CategoryVO> categoryList) {
		super(context);
		this.categoryList = categoryList;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category_list);
		setViews();
		setListeners();
		refreshList();
	}
	
	public void onMeasure(View parent ,int width,int height,int statusHeight){
		
		//首先获得父控件的坐标和大小
		int ph = parent.getHeight();
		int pw = parent.getWidth();
		int ptop = parent.getTop();
		int pleft = parent.getLeft();
		//获得Dialog的坐标和大小
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		//获得屏幕的高和宽
		Display display = getWindow().getWindowManager().getDefaultDisplay();
		int screenHeight = display.getHeight();
		int screenWidth = display.getWidth();
		//获得状态栏高度
		
		int newY = screenHeight/2-statusHeight-(ptop+ph)-height/2;
		int newX = screenWidth/2-width/2;
		lp.x = newX;
		lp.y = -newY;
		lp.height = height;
		lp.width = width;
		getWindow().setAttributes(lp);
		L.d(pleft + " " + ptop + " " + pw + " " + ph);
		L.d(newX + " " + newY + " " + lp.width + " " + height);
	}
	
	private void refreshList(){
		if(categoryList==null)
			return;
		adapter = new CategoryAdapter();
		listView.setAdapter(adapter);
	}
	
	public void setViews(){
		listView = (ListView)findViewById(R.id.categorylist);
	}
	
	public void setListeners(){
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				L.d("clik");
				if(onDialogItemClickListener!=null)
					onDialogItemClickListener.onItemClick(view.getTag().toString());
			}
		});
	}
	
	private class CategoryAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return categoryList.size();
		}

		@Override
		public Object getItem(int position) {
			return categoryList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			CategoryVO categoryVO = categoryList.get(position);
			if(view==null)
				view = LayoutInflater.from(getContext()).inflate(R.layout.categorylist_item, null);
			TextView categoryName = (TextView)view.findViewById(R.id.categoryname);
			categoryName.setText(categoryVO.getName());
			view.setTag(categoryVO.getId());
			L.d(categoryVO.getName());
			return view;
		}
		
	}
	
	public interface OnDialogItemClickListener{
		public void onItemClick(String id);
	}

	public OnDialogItemClickListener getOnDialogItemClickListener() {
		return onDialogItemClickListener;
	}

	public void setOnDialogItemClickListener(
			OnDialogItemClickListener onDialogItemClickListener) {
		this.onDialogItemClickListener = onDialogItemClickListener;
	}
	
	
}
